package xdTool

import sysAuth.SysNoticeController
import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysCommUtil
import xdInterface.XdController

class XdTableColCfgController extends XdController{

    def sysDBUtilService
    def authUtilsService

    @Override
    def detail() {
        return null
    }

    @Override
    def del() {
        return null
    }

    @Override
    def edit() {
        return null
    }

    @Override
    def save() {
        return null
    }

    @Override
    def update() {
        def result = ResultJson.resultJson()
        try {
            def colId = Long.parseLong(params?.id)
            XdTableColCfg  xdTableColCfg = XdTableColCfg.get(colId)
            if(!xdTableColCfg){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {
                xdTableColCfg.setProperty("field",params?.field)
                xdTableColCfg.setProperty("title",params?.title )
                xdTableColCfg.setProperty("type",params.type?:"normal")
                xdTableColCfg.setProperty("fixed",params?.fixed )
                xdTableColCfg.setProperty("width",params?.width )
                xdTableColCfg.setProperty("minWidth",params?.minWidth)

                xdTableColCfg.setProperty("align",params.align?:"left")
                xdTableColCfg.setProperty("rowspan",params.rowspan?:"1")
                xdTableColCfg.setProperty("colspan",SysCommUtil.String2Clob(params.colspan?:"1"))
                xdTableColCfg.setProperty("templet",SysCommUtil.String2Clob(params?.templet)  )
                xdTableColCfg.setProperty("style",params?.style )
                xdTableColCfg.setProperty("totalRow",params.totalRow?:"false")
                xdTableColCfg.setProperty("totalRowText",params?.totalRowText)
                xdTableColCfg.setProperty("mergeFlag",params.mergeFlag?:"false")
                xdTableColCfg.setProperty("mergeRefField",params?.mergeRefField)

                xdTableColCfg.setProperty("dataType",params.dataType?:"String")
                xdTableColCfg.setProperty("primaryKey",params?.primaryKey)
                xdTableColCfg.setProperty("primaryValue",params?.primaryValue)
                xdTableColCfg.setProperty("combKey",params.combKey?:"false")

                xdTableColCfg.setProperty("hide",params.hide?:"false")
                xdTableColCfg.setProperty("sort",params.sort?:"false")

                xdTableColCfg.setProperty("paramId",params.paramId?Long.parseLong(params.paramId):null )

                xdTableColCfg.save(flush: true)

                def cols = XdTableColCfg.findAll("from XdTableColCfg as a where a.xdTableId=:xdTableId order by a.grade,a.colIndex",[xdTableId:xdTableColCfg.xdTableId])

                result.put(ResultJson.DATA.name,SysCommUtil.copyMapList(cols))
                result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result.toString()
        }
    }


    @Override
    def add() {
        def result = ResultJson.resultJson()
        try {
            def xdTableId = Long.parseLong(params?.xdTableId)
            XdTableCfg xdTableCfg = XdTableCfg.get(xdTableId)
            if(!xdTableCfg){
                result.put(ResultJson.CODE.name,ResultCode.OPT_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_ERROR.message)
            }else {
                XdTableColCfg xdTableColCfg = XdTableColCfg.newInstance()
                def grade = Integer.parseInt(params.grade?:"1")
                def colIndex = XdTableColCfg.countByXdTableIdAndGrade(xdTableId,grade) + 1
                def field = params?.field
                if(field){
                    xdTableColCfg.setProperty("field",field)
                }else {
                    xdTableColCfg.setProperty("title","Field${colIndex}")
                }

                xdTableColCfg.setProperty("xdTableId",xdTableId)
                xdTableColCfg.setProperty("creator",params.userNo)
                xdTableColCfg.setProperty("colIndex",colIndex)
                xdTableColCfg.setProperty("grade",grade)
                xdTableColCfg.save(flush: true)
                result.put(ResultJson.DATA.name,SysCommUtil.copyMap(xdTableColCfg))
                result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result.toString()
        }
    }

    /**
     * 更新字段展示顺序
     * @return
     */
    def setIndex(){
        def result = ResultJson.resultJson()
        try {
            def colId = Long.parseLong(params?.id)
            def reduce = Integer.parseInt(params?.reduce)

            XdTableColCfg xdTableColCfg = XdTableColCfg.get(colId)
            if(!xdTableColCfg){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {

                def grade = xdTableColCfg.grade
                def xdTableId = xdTableColCfg.xdTableId

                if(reduce == 0 ){

                    xdTableColCfg.delete(flush: true)
                    //刷新字段索引
                    XdTableColCfg.findAll("from XdTableColCfg as a where a.xdTableId= :xdTableId and a.grade = :grade order by a.colIndex",
                    [xdTableId:xdTableId,grade:grade])?.eachWithIndex {item,index ->
                        item.setColIndex(index+1)
                        item.save(flush: true)
                    }

                    //刷新表头级别 grade
                    def colsAll =  XdTableColCfg.findAll("from XdTableColCfg as a where a.xdTableId = ${xdTableId}  order by a.grade ,a.colIndex" )

                    def grades = colsAll?.grade?.unique()
                    if(grades && !grades.contains(grade)){
                        grades.eachWithIndex { def entry, int i ->
                            XdTableColCfg.findAllByXdTableIdAndGrade(xdTableId,entry)?.each {
                                it.setGrade(i+1)
                                it.save(flush: true)
                            }
                        }
                    }

                    colsAll = XdTableColCfg.findAll("from XdTableColCfg as a where a.xdTableId = ${xdTableId}  order by a.grade ,a.colIndex" )

                    result.put(ResultJson.DATA.name,SysCommUtil.copyMapList(colsAll))
                    result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)

                }else{

                    def oldIndex = xdTableColCfg.colIndex

                    def newIndex = oldIndex + reduce
                    XdTableColCfg oldColCfg = XdTableColCfg.findByXdTableIdAndColIndexAndGrade(xdTableColCfg.xdTableId,newIndex,grade)
                    if(oldColCfg){//判定是否为开始和结束

                        xdTableColCfg.setColIndex(newIndex)
                        oldColCfg.setColIndex(oldIndex)
                        xdTableColCfg.save(flush: true)
                        oldColCfg.save(flush: true)
                        result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                        result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                    }else{
                        result.put(ResultJson.CODE.name,ResultCode.OPT_ERROR.code)
                        result.put(ResultJson.MSG.name,ResultCode.OPT_ERROR.message)
                    }
                }
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result.toString()
        }
    }

    @Override
    def dowLoad() {
        return null
    }

    @Override
    def upLoad() {
        return null
    }

    @Override
    def getOne() {
        def result = ResultJson.resultJson()
        try {
            def colId = Long.parseLong(params?.id)
            XdTableColCfg xdTableColCfg = XdTableColCfg.get(colId)
            if(!xdTableColCfg){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {
                result.put(ResultJson.DATA.name,SysCommUtil.copyMap(xdTableColCfg))
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result.toString()
        }
    }

    @Override
    def query() {
        return null
    }

    @Override
    def delete() {
        return null
    }

    /**
     * 当合并行时，需选择一个参照列，页面下拉框
     */
    def getFieldSelect(){
        def result = ResultJson.resultJson()
        try {//字段ID
            def colId = Long.parseLong(params?.id)
            XdTableColCfg xdTableColCfg = XdTableColCfg.get(colId)

            def cols = XdTableColCfg.findAll("from XdTableColCfg as a where a.xdTableId = :xdTableId and a.field is not null order by a.colIndex",[xdTableId:xdTableColCfg.xdTableId])
            def data = []
            cols?.each {
                def item = [name:it.title?:it.field,value:it.field]
                if(!data.contains(item) && it.id != colId){
                    data.add(item)
                }
            }

            result.put(ResultJson.DATA.name,data)
            result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
            result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)

        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result.toString()
        }
    }
}
